package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.common.utils.Tools;
import com.ld.hpfs.dao.UtilMapper;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.pojo.ExtReturn;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.HistoryService;
import com.ld.hpfs.service.MdataService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class MdataController {
	private static final Logger logger = LoggerFactory.getLogger(MdataController.class);	
	@Autowired
    private MdataService      mdataService;
	@Autowired
    private UtilMapper          utilMapper;
	
	//用于添加history表
	@Autowired
    private HistoryService      historyService;
	@Autowired
	private EnterInvoiceController enterinvoicecontroller;
	/*
	@RequestMapping(value = "/addmdatatype",method = RequestMethod.POST)
	@ResponseBody
	public Object addMdataType(@RequestParam String mdatatypecode, @RequestParam String mdatatype,
			HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆!");
        	} else {
        		String orderBy = "DOC_TYPE_CODE";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("mdatatypecode", mdatatypecode);
        		param.put("mdatatype", mdatatype);
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			
    			int id = mdataService.getMdataTypeCodeFromMdataType(param);
    			if (id != -1)
    			{
    				return new ExtReturn(false, "票据类型代码已存在!");
    			}else {
    				String iResult = mdataService.addMdataTypeCode(param);
    				if(iResult.equals("-1")){
    					return new ExtReturn(false, "添加票据类型代码失败!");
				         } 
    			}
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/updatemdatatype",method = RequestMethod.POST)
	@ResponseBody
	public Object UpdateMdataType(@RequestParam String mdatatypecode, @RequestParam String mdatatype,
			@RequestParam String id,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆!");
        	} else {
        		String orderBy = "DOC_TYPE_CODE";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("mdatatypecode", mdatatypecode);
        		param.put("mdatatype", mdatatype);
        		param.put("id",id);
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			
    			String iResult = mdataService.updateMdataTypeCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "修改票据类型代码失败!");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/deletemdatatype",method = RequestMethod.POST)
	@ResponseBody
	public Object DeleteMdataType(@RequestParam String id,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆!");
        	} else {
        		param.put("id",id);
    			
    			String iResult = mdataService.deleteMdataTypeCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "删除票据类型代码失败!");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}*/
	
	@RequestMapping(value = "/addmdataaccount",method = RequestMethod.POST)
	@ResponseBody
	public Object addmdataaccount(@RequestParam String mdataaccountcode, @RequestParam String mdataaccount,
			HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			Random random = new Random();
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "DOC_TYPE_CODE";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("mdataaccountcode", mdataaccountcode);
        		param.put("mdataaccount", mdataaccount);
        		param.put("mdatadefaultaccount", 0);
        		param.put("mdatabankname","ChinaBank");
        		param.put("mdatasellerid", random.nextInt(9999999));
        		param.put("companyId", session.getAttribute(WebConstants.CURRENT_COMPANY));
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
    			int id = mdataService.getMdataAccountCodeFromMdataAccount(param);
    			if (id != -1)
    			{
    				return new ExtReturn(false, "销方银行账号代码已存在。");
    			}else {
    				String iResult = mdataService.addMdataAccountCode(param);
    				if(iResult.equals("-1")){
    					return new ExtReturn(false, "添加销方银行账号代码失败。");
				         } 
    			}
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/updatemdataaccount",method = RequestMethod.POST)
	@ResponseBody
	public Object Updatemdataaccount(@RequestParam String mdataaccountcode, @RequestParam String mdataaccount,
			@RequestParam String id,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "DOC_TYPE_CODE";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("mdataaccountcode", mdataaccountcode);
        		param.put("mdataaccount", mdataaccount);
        		param.put("id",id);
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
				// 用于添加history表
				HashMap<String,Object> history_param = new HashMap<String,Object>();
        		history_param.put("id", "(" + id + ")");
        		history_param.put("groupId", groupId);
        		history_param.put("update_id", Integer.parseInt(user.getUserId()));
				history_param.put("update_name", user.getAccount());
				historyService.addMdataBankAccountHistory(history_param, true);
				
    			String iResult = mdataService.updateMdataAccountCode(param);
				
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "修改销方银行账号代码失败。");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/deletemdataaccount",method = RequestMethod.POST)
	@ResponseBody
	public Object Deletemdataaccount(@RequestParam String id,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("id",id);
        		param.put("groupId", groupId);
    			
        		// 用于添加history表
        		param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				historyService.addMdataBankAccountHistory(param, false);
        		
    			String iResult = mdataService.deleteMdataAccountCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "删除销方银行账号代码失败。");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/addmdatagoods",method = RequestMethod.POST)
	@ResponseBody
	public Object AddMdataGoods(@RequestParam String goodscode, @RequestParam String goodsname, @RequestParam String goodsitem,
			@RequestParam String goodsrate, @RequestParam String goodsspeci, @RequestParam String goodsmeas, @RequestParam String goodsprice,
			@RequestParam String goodsshort, HttpSession session, HttpServletRequest request){
		    HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "GOODS_ID";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("goodscode", goodscode.trim()==""?null:goodscode.trim());
        		param.put("goodsname", goodsname.trim()==""?null:goodsname.trim());
        		param.put("goodsitem", goodsitem.trim()==""?null:goodsitem.trim());
        		param.put("goodsrate", goodsrate.trim()==""?null:goodsrate.trim());
        		param.put("goodsspeci", goodsspeci.trim()==""?null:goodsspeci.trim());
        		param.put("goodsmeas", goodsmeas.trim()==""?null:goodsmeas.trim());
        		param.put("goodsprice", goodsprice.trim()==""?null:goodsprice.trim());
        		param.put("goodsshort", goodsshort.trim()==""?null:goodsshort.trim());
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
    			int id = mdataService.getMdataGoodsCodeFromMdataGoods(param);
    			if (id != -1)
    			{
    				return new ExtReturn(false, "商品代码已存在。");
    			}else {
    				String iResult = mdataService.addMdataGoodsCode(param);
    				if(iResult.equals("-1")){
    					return new ExtReturn(false, "添加商品代码失败。");
				         } 
    			}
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/updatemdatagoods",method = RequestMethod.POST)
	@ResponseBody
	public Object UpdateMdataGoods(@RequestParam String goodscode, @RequestParam String goodsname, @RequestParam String goodsitem,
			@RequestParam String goodsrate, @RequestParam String goodsspeci, @RequestParam String goodsmeas, @RequestParam String goodsprice,
			@RequestParam String goodsshort, @RequestParam String id, HttpSession session, HttpServletRequest request){
		    HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "GOODS_ID";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("goodscode", goodscode.trim()==""?null:goodscode.trim());
        		param.put("goodsname", goodsname.trim()==""?null:goodsname.trim());
        		param.put("goodsitem", goodsitem.trim()==""?null:goodsitem.trim());
        		param.put("goodsrate", goodsrate.trim()==""?null:goodsrate.trim());
        		param.put("goodsspeci", goodsspeci.trim()==""?null:goodsspeci.trim());
        		param.put("goodsmeas", goodsmeas.trim()==""?null:goodsmeas.trim());
        		param.put("goodsprice", goodsprice.trim()==""?null:goodsprice.trim());
        		param.put("goodsshort", goodsshort.trim()==""?null:goodsshort.trim());
        		param.put("id", id);
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
				// 用于添加history表
				HashMap<String,Object> history_param = new HashMap<String,Object>();
        		history_param.put("id", "(" + id + ")");
        		history_param.put("groupId", groupId);
        		history_param.put("update_id", Integer.parseInt(user.getUserId()));
				history_param.put("update_name", user.getAccount());
				historyService.addMdataGoodHistory(history_param, true);
				
    			String iResult = mdataService.updateMdataGoodsCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "修改商品代码失败。");
    			} 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/deletemdatagoods",method = RequestMethod.POST)
	@ResponseBody
	public Object DeleteMdataGoods(@RequestParam String id,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("id",id);
        		param.put("groupId", groupId);
    			
        		// 用于添加history表
        		param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				historyService.addMdataGoodHistory(param, false);
        		
    			String iResult = mdataService.deleteMdataGoodsCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "删除商品代码失败。");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/addmdataclient",method = RequestMethod.POST)
	@ResponseBody
	public Object AddMdataClient(@RequestParam String clientcode, @RequestParam String clientsupplier,
			@RequestParam String clienttax,@RequestParam String address, @RequestParam String bankaccount,
			@RequestParam String clientbank,@RequestParam String clienttel,@RequestParam String invoiceType,
			@RequestParam String ck01,@RequestParam String ck02,
			@RequestParam String ck03,@RequestParam String ck04,@RequestParam String ck05,@RequestParam String ck06,
			@RequestParam String ck07,@RequestParam String ck08,@RequestParam String ck09,@RequestParam String ck10,
			HttpSession session, HttpServletRequest request){
		    HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "CUST_ID";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("clientcode", clientcode);
        		param.put("clientsupplier", clientsupplier);
        		param.put("clienttax", clienttax);
        		param.put("address", address);
        		param.put("bankaccount", bankaccount);
        		param.put("clientbank", clientbank);
        		param.put("clienttel", clienttel);
        		param.put("invoicetype", invoiceType);
        		param.put("ck01", ck01);
        		param.put("ck02", ck02);
        		param.put("ck03", ck03);
        		param.put("ck04", ck04);
        		param.put("ck05", ck05);
        		param.put("ck06", ck06);
        		param.put("ck07", ck07);
        		param.put("ck08", ck08);
        		param.put("ck09", ck09);
        		param.put("ck10", ck10);
        		param.put("upid", user.getUserId());
        		param.put("upname", "LZQ");
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
    			int id = mdataService.getMdataClientCodeFromMdataClient(param);
    			if (id != -1)
    			{
    				return new ExtReturn(false, "客户代码已存在。");
    			}else {
    				String iResult = mdataService.addMdataClientCode(param);
    				if(iResult.equals("-1")){
    					return new ExtReturn(false, "添加客户代码失败。");
				         } 
    			}
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/updatemdataclient",method = RequestMethod.POST)
	@ResponseBody
	public Object UpdateMdataClient(@RequestParam String clientcode, @RequestParam String clientsupplier,
			@RequestParam String clienttax,@RequestParam String address, @RequestParam String bankaccount,
			@RequestParam String clientbank,@RequestParam String clienttel,@RequestParam String invoiceType,@RequestParam String ck01,@RequestParam String ck02,
			@RequestParam String ck03,@RequestParam String ck04,@RequestParam String ck05,@RequestParam String ck06,
			@RequestParam String ck07,@RequestParam String ck08,@RequestParam String ck09,@RequestParam String ck10, 
			@RequestParam String id,HttpSession session, HttpServletRequest request){
		    HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "CUST_ID";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("clientcode", clientcode);
        		param.put("clientsupplier", clientsupplier);
        		param.put("clienttax", clienttax);
        		param.put("address", address);
        		param.put("bankaccount", bankaccount);
        		param.put("id", id);
        		param.put("clientbank", clientbank);
        		param.put("clienttel", clienttel);
        		param.put("invoicetype", invoiceType);
        		param.put("ck01", ck01);
        		param.put("ck02", ck02);
        		param.put("ck03", ck03);
        		param.put("ck04", ck04);
        		param.put("ck05", ck05);
        		param.put("ck06", ck06);
        		param.put("ck07", ck07);
        		param.put("ck08", ck08);
        		param.put("ck09", ck09);
        		param.put("ck10", ck10);
        		param.put("upid", user.getUserId());
        		param.put("upname", "LZQ");
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
				// 用于添加history表
				param.put("ids", "(" + id + ")");
				historyService.addMdataClientHistory(param, true);
    			
				String iResult = mdataService.updateMdataClientCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "修改客户代码失败。");
    			} 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/deletemdataclient",method = RequestMethod.POST)
	@ResponseBody
	public Object DeleteMdataClient(@RequestParam String ids,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		param.put("ids",ids);
        		
        		// 用于添加history表
        		param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				historyService.addMdataClientHistory(param, false);
    			
    			String iResult = mdataService.deleteMdataClientCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "删除客户代码失败。");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/addmdataclientaddress",method = RequestMethod.POST)
	@ResponseBody
	public Object AddMdataClientAddress(@RequestParam String recv_company, @RequestParam String recv_man,
			@RequestParam String recv_address,@RequestParam String recv_tel, @RequestParam String city,@RequestParam String recv_remark,
			@RequestParam String recv_email,@RequestParam String recv_phone,@RequestParam String recv_ck01,@RequestParam String recv_ck02,@RequestParam String recv_ck03,@RequestParam String recv_ck04,
			@RequestParam String recv_ck05,@RequestParam String cust_id,HttpSession session, HttpServletRequest request){
		    HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "CUST_ID";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("cust_id", cust_id);
        		param.put("recv_company", recv_company);
        		param.put("recv_man", recv_man);
        		param.put("recv_address", recv_address);
        		param.put("recv_tel", recv_tel);
        		param.put("city", city);
        		param.put("create_time", Tools.getCurrentDay());
        		param.put("recv_remark", recv_remark);
        		param.put("recv_email", recv_email);
        		param.put("recv_phone", recv_phone);
        		param.put("recv_ck01", recv_ck01);
        		param.put("recv_ck02", recv_ck02);
        		param.put("recv_ck03", recv_ck03);
        		param.put("recv_ck04", recv_ck04);
        		param.put("recv_ck05", recv_ck05);
        		param.put("upid", user.getUserId());
        		param.put("upname", "LZQ");
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
    			String id = mdataService.getMdataClientAddressCodeFromMdataClient(param);
    			if (!id.equals("NODATA"))
    			{
    				return new ExtReturn(false, "相同客户联系方式已存在。");
    			}else {
    				String iResult = mdataService.addMdataClientAddressCode(param);
    				if(iResult.equals("-1")){
    					return new ExtReturn(false, "添加客户联系方式失败。");
				         } 
    			}
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/updatemdataclientaddress",method = RequestMethod.POST)
	@ResponseBody
	public Object UpdateMdataClientAddress(@RequestParam String recv_company, @RequestParam String recv_man,
			@RequestParam String recv_address,@RequestParam String recv_tel, @RequestParam String city,
			@RequestParam String recv_remark,@RequestParam String recv_email,@RequestParam String recv_phone,@RequestParam String recv_ck01,@RequestParam String recv_ck02,@RequestParam String recv_ck03,
			@RequestParam String recv_ck04,@RequestParam String recv_ck05,@RequestParam String cust_id,@RequestParam String id,
			HttpSession session, HttpServletRequest request){
		    HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		String orderBy = "CUST_ID";
        		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
        		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
        		param.put("cust_id", cust_id);
        		param.put("id", id);
        		param.put("recv_company", recv_company);
        		param.put("recv_man", recv_man);
        		param.put("recv_address", recv_address);
        		param.put("recv_tel", recv_tel);
        		param.put("city", city);
        		param.put("recv_remark", recv_remark);
        		param.put("recv_email", recv_email);
        		param.put("recv_phone", recv_phone);
        		param.put("recv_ck01", recv_ck01);
        		param.put("recv_ck02", recv_ck02);
        		param.put("recv_ck03", recv_ck03);
        		param.put("recv_ck04", recv_ck04);
        		param.put("recv_ck05", recv_ck05);
        		param.put("upid", user.getUserId());
        		param.put("upname", "LZQ");
    			param.put("groupId", groupId);
    			param.put("orderBy", orderBy);
    			//更新update_id,update_name信息 by guxiaoli
				param.put("update_id", Integer.parseInt(user.getUserId()));
				param.put("update_name", user.getAccount());
				
				//用于添加history表
        		HashMap<String,Object> param_history = new HashMap<String,Object>();
        		param_history.put("contact_ids", "(" + id + ")");
        		param_history.put("update_id", Integer.parseInt(user.getUserId()));
				param_history.put("update_name", user.getAccount());
				historyService.addMdataClientContactHistory(param_history, true);
				
    			String iResult = mdataService.updateMdataClientAddressCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "修改客户联系方式失败。");
			    } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	
	@RequestMapping(value = "/deletemdataclientaddress",method = RequestMethod.POST)
	@ResponseBody
	public Object DeleteMdataClientAddress(@RequestParam String ids,HttpSession session, HttpServletRequest request){
		HashMap<String,Object> param = new HashMap<String,Object>();
		try{
			User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
        	if (user == null)
        	{
        		return new ExtReturn(false, "未登陆。");
        	} else {
        		param.put("ids",ids);
//        		param.put("cust_id",cust_id);
    			
        		//用于添加history表
        		HashMap<String,Object> param_history = new HashMap<String,Object>();
        		param_history.put("contact_ids", ids);
        		param_history.put("update_id", Integer.parseInt(user.getUserId()));
				param_history.put("update_name", user.getAccount());
				historyService.addMdataClientContactHistory(param_history, false);
        		
        		
    			String iResult = mdataService.deleteMdataClientAddressCode(param);
    			if(iResult.equals("-1")){
    				return new ExtReturn(false, "删除客户联系信息失败。");
				 } 
        	}
		}catch (Exception e){
			logger.error("Exception: ", e);
		}
		return new ExtReturn(true, "success");
	}
	/*
	@RequestMapping(value = "/getMdataType")
    @ResponseBody
    public void getMdataType(ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer)
    {
    	try{
    		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
    		if (currentCompany == null)
    		{
    			return;
    		}
        
    		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
    		// String sort = (String)request.getParameter("sort") == null ? null :
    		// (String)request.getParameter("sort");
    		int pageStart = page.getStart();
    		int pageEnd = page.getStart() + page.getLimit();
    		String orderby = "DOC_TYPE_ID";
        
    		HashMap<String, Object> param = new HashMap<String, Object>();
    		param.put("pageStart", pageStart);
    		param.put("pageEnd", pageEnd);
    		param.put("orderBy", orderby);
    		param.put("groupId", groupId);
        
    		ArrayList<HashMap<String,Object>> result = mdataService.getMdataType(param);       

    		String returnMsg = JackJson.fromObjectToJson(result);
    		logger.debug("{}", returnMsg);
    		writer.print(returnMsg);
    	}catch (Exception e){
    		logger.error("Exception: ", e);
    	}finally{
    		writer.flush();
			writer.close();
    	}
    }
    */
    @RequestMapping(value = "/getMdataAccount")
    @ResponseBody
    public void getMdataAccount(ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer)
    {
    	try{
    		String searchInfo = (String)request.getParameter("searchInfo") == null ? "" : (String)request.getParameter("searchInfo");
    		HashMap<String,Object> result = new HashMap<String,Object>();//用于返回的结果集
    		Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
    		if (currentCompany == null)
    		{
    			return;
    		}
        
    		int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
    		// String sort = (String)request.getParameter("sort") == null ? null :
    		// (String)request.getParameter("sort");
    		int pageStart = page.getStart();
    		int pageEnd = page.getStart() + page.getLimit();
    		String orderby = "SELLER_CODE";
        
    		HashMap<String, Object> param = new HashMap<String, Object>();
    		param.put("pageStart", pageStart);
    		param.put("pageEnd", pageEnd);
    		param.put("orderBy", orderby);
    		param.put("groupId", groupId);
    		if(searchInfo.length() >= 1){
				searchInfo  = "%" + searchInfo + "%";
				param.put("searchInfo", searchInfo);
			}
        
    		ArrayList<HashMap<String,Object>> list = mdataService.getMdataAccount(param);       

			if(list!=null&&list.size()>0){//准备前台所需的各种数据
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", list);
			} else {
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", "");
			}
    		String returnMsg = JackJson.fromObjectToJson(result);
    		logger.debug("{}", returnMsg);
    		writer.print(returnMsg);
    	}catch (Exception e){
    		logger.error("Exception: ", e);
    	}finally{
    		writer.flush();
			writer.close();
    	}
    }
	
	@RequestMapping(value = "/getMdataGoodsList", method = RequestMethod.GET)
    @ResponseBody
    public void getMdataGoodsList(ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer)
    {
				response.setContentType("text/plain;charset=UTF-8");
		try{
			String searchInfo = (String)request.getParameter("searchInfo") == null ? "" : (String)request.getParameter("searchInfo");
			Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			if (currentCompany == null)
			{
				return;
			}
        
			int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
			// String sort = (String)request.getParameter("sort") == null ? null :
			// (String)request.getParameter("sort");
			int pageStart = page.getStart();
			int pageEnd = page.getStart() + page.getLimit();
			String orderby = "GOODS_ID";
        
			HashMap<String, Object> param = new HashMap<String, Object>();
			HashMap<String, Object> result = new HashMap<String, Object>();
			param.put("pageStart", pageStart);
			param.put("pageEnd", pageEnd);
			param.put("orderBy", orderby);
			param.put("groupId", groupId);
			if(searchInfo.length() >= 1){
				searchInfo  = "%" + searchInfo + "%";
				param.put("searchInfo", searchInfo);
			}
        
			ArrayList<HashMap<String,Object>> logs = mdataService.getMdataGoodsList(param);
			if(logs!=null&&logs.size()>0){//准备前台所需的各种数据
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", logs);
			} else {
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", "");
			}
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		}catch (Exception e){
			logger.error("Exception: ", e);
		}finally{
			writer.flush();
			writer.close();
		}
    }
	
	@RequestMapping(value = "/getMdataClientList")
    @ResponseBody
    public void getMdataClientList(ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer)
    {
		try{
			String searchInfo = (String)request.getParameter("searchInfo") == null ? "" : (String)request.getParameter("searchInfo");
			HashMap<String,Object> result = new HashMap<String,Object>();//用于返回的结果集
			Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			if (currentCompany == null)
			{
				return;
			}
			String language = (String) session.getAttribute(WebConstants.CURRENT_LANG);
			int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
			// String sort = (String)request.getParameter("sort") == null ? null :
			// (String)request.getParameter("sort");
			int pageStart = page.getStart();
			int pageEnd = page.getStart() + page.getLimit();
			String orderby = "CUST_ID";
        
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("pageStart", pageStart);
			param.put("pageEnd", pageEnd);
			param.put("orderBy", orderby);
			param.put("groupId", groupId);
			param.put("language", language);
			if(searchInfo.length() >= 1){
				searchInfo  = "%" + searchInfo + "%";
				param.put("searchInfo", searchInfo);
			}
        
			ArrayList<HashMap<String,Object>>logs = mdataService.getMdataClientList(param);

			if(logs!=null&&logs.size()>0){//准备前台所需的各种数据
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", logs);
			} else {
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", "");
			}
			String returnMsg = JackJson.fromObjectToJson(result);
	        logger.debug("{}", returnMsg);
	        writer.print(returnMsg);
		}catch (Exception e){
			logger.error("Exception: ", e);
		}finally{
			writer.flush();
			writer.close();
		}
    }
	
	@RequestMapping(value = "/getMdataClientAddressList"/*,method = RequestMethod.GET*/)
    @ResponseBody
    public void getMdataClientAddressList(/*@RequestParam String mainId,*/ExtPager page, HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer)
    {
		try{
			String mainId = (String)request.getParameter("mainId") == null ? "" : (String)request.getParameter("mainId");
			Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			if (currentCompany == null)
			{
				return;
			}
        
			int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
			// String sort = (String)request.getParameter("sort") == null ? null :
			// (String)request.getParameter("sort");
			int pageStart = page.getStart();
			int pageEnd = page.getStart() + page.getLimit();
			String orderby = "CONTACT_ID";
        
			HashMap<String, Object> param = new HashMap<String, Object>();
			HashMap<String, Object> result = new HashMap<String, Object>();
			param.put("pageStart", pageStart);
			param.put("pageEnd", pageEnd);
			param.put("orderBy", orderby);
			param.put("groupId", groupId);
			param.put("mainId", mainId);
        
			ArrayList<HashMap<String,Object>>logs = mdataService.getMdataClientAddressList(param);
			if(logs!=null&&logs.size()>0){//准备前台所需的各种数据
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", logs);
			} else {
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", "");
			}
			String returnMsg = JackJson.fromObjectToJson(result);
	        logger.debug("{}", returnMsg);
	        writer.print(returnMsg);
		}catch (Exception e){
			logger.error("Exception: ", e);
		}finally{
			writer.flush();
			writer.close();
		}
    }
	
	@RequestMapping(value = "/getNewCustCode")
	public void getNewCustCode(ExtPager page, HttpSession session,
			HttpServletRequest request, HttpServletResponse response,
			PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		try {
			HashMap<String,Object> result = new HashMap<String,Object>();
			String custCode = enterinvoicecontroller.getCustCode();
			result.put("custCode", custCode);
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
	
	@RequestMapping(value = "/getMdataAcc")
	public void getMdataAcc(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, PrintWriter writer) {
			HashMap<String, Object> param = new HashMap<String,Object>();
		try {
			Object currentCompany = session.getAttribute(WebConstants.CURRENT_COMPANY);
			if (currentCompany == null)
			{
				return;
			}
			// 主数据套
			int groupId = utilMapper.getGroupIdFromCompanyId(Integer.parseInt(currentCompany.toString()));
			param.put("groupId", groupId);
			ArrayList<HashMap<String,String>> MdataAcc = mdataService.getMdataAcc(param);
			String returnMsg = JackJson.fromObjectToJson(MdataAcc);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: {}", e);
		}
	}
}
